import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../provide/userProvide.dart';
import 'package:provide/provide.dart';
import '../../routers/application.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromRGBO(236, 238, 239, 1.0),
      body: ListView(
        children: <Widget>[_topHeader(), _orderTitle(context), _actionList()],
      ),
    );
  }

  Widget _topHeader() {
    return Container(
      width: ScreenUtil().setWidth(750),
      color: Colors.pink,
      padding: EdgeInsets.only(top: 84.0, bottom: 38.0),
      child: Provide<UserProvide>(builder: (context, child, val) {
        var userInfo = Provide.value<UserProvide>(context).userIofo;

        if (userInfo != null) {
          return Container(
              child: Row(
            children: <Widget>[
              Container(
                width: 60.0,
                height: 60.0,
                margin: EdgeInsets.only(left: 18.0, right: 14.0),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(100),
                    image: DecorationImage(
                      image: NetworkImage(userInfo.avatar),
                      fit: BoxFit.cover,
                    )),
              ),
              Container(
                  margin: EdgeInsets.only(right: 100.0),
                  child: Text(
                    userInfo.name,
                    style: TextStyle(
                      fontSize: 22.0, // 文字大小
                      color: Colors.white, // 文字颜色
                    ),
                  )),
              OutlineButton(
                  onPressed: () async {
                    SharedPreferences prefs =
                        await SharedPreferences.getInstance();
                    prefs.clear();
                    Provide.value<UserProvide>(context).removeInfo();
                    Application.router.navigateTo(context, "/login");
                  },
                  highlightColor: Colors.pink[100],
                  borderSide:
                      BorderSide(color: Color.fromRGBO(255, 255, 255, 1)),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(14))),
                  child: Text(
                    "退出",
                    textAlign: TextAlign.right,
                    style: TextStyle(
                        fontSize: 14.0,
                        fontWeight: FontWeight.w500,
                        color: Color.fromRGBO(255, 255, 255, 1)),
                  ))
            ],
          ));
        } else {
          return Container(
              child: Row(
            children: <Widget>[
              Container(
                width: 60.0,
                height: 60.0,
                margin: EdgeInsets.only(left: 18.0, right: 14.0),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(100),
                    image: DecorationImage(
                      image: NetworkImage(
                          'http://www.xlmx.xyz:8090/static/img/user/logo.jpg'),
                      fit: BoxFit.cover,
                    )),
              ),
              InkWell(
                  onTap: () {
                    Application.router.navigateTo(context, "/login");
                  },
                  child: Text(
                    "点击登录",
                    style: TextStyle(
                      fontSize: 22.0, // 文字大小
                      color: Colors.white, // 文字颜色
                    ),
                  ))
            ],
          ));
        }
      }),

      // Container(
      //   margin: EdgeInsets.only(top: 30),

      //   builder: (context,snapshot){},
      //    ClipOval(child: Image.network('avatar')),
      // ),
    );
  }

  //我的订单顶部
  Widget _orderTitle(context) {
    return Container(
      margin: EdgeInsets.only(top: 10),
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border(bottom: BorderSide(width: 1, color: Colors.black12))),
      child: ListTile(
        onTap: () {
          // var data = Provide.value<UserProvide>(context).userIofo;
          // if (data == null) {
          //   Application.router.navigateTo(context, "/login");
          // } else {
          Application.router.navigateTo(context, "/myOrder");
          // }
        },
        leading: Icon(Icons.list),
        title: Text('我的订单'),
        trailing: Icon(Icons.arrow_right),
      ),
    );
  }

  Widget _orderType() {
    return Container(
      margin: EdgeInsets.only(top: 5),
      width: ScreenUtil().setWidth(750),
      height: ScreenUtil().setHeight(150),
      padding: EdgeInsets.only(top: 20),
      color: Colors.white,
      child: Row(
        children: <Widget>[
          Container(
            width: ScreenUtil().setWidth(187),
            child: Column(
              children: <Widget>[
                Icon(
                  Icons.party_mode,
                  size: 30,
                ),
                Text('待付款'),
              ],
            ),
          ),
          //-----------------
          Container(
            width: ScreenUtil().setWidth(187),
            child: Column(
              children: <Widget>[
                Icon(
                  Icons.query_builder,
                  size: 30,
                ),
                Text('待发货'),
              ],
            ),
          ),
          //-----------------
          Container(
            width: ScreenUtil().setWidth(187),
            child: Column(
              children: <Widget>[
                Icon(
                  Icons.directions_car,
                  size: 30,
                ),
                Text('待收货'),
              ],
            ),
          ),
          Container(
            width: ScreenUtil().setWidth(187),
            child: Column(
              children: <Widget>[
                Icon(
                  Icons.content_paste,
                  size: 30,
                ),
                Text('待评价'),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _myListTile(String title) {
    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border(bottom: BorderSide(width: 1, color: Colors.black12))),
      child: ListTile(
        leading: Icon(Icons.blur_circular),
        title: Text(title),
        trailing: Icon(Icons.arrow_right),
      ),
    );
  }

  Widget _actionList() {
    return Container(
      margin: EdgeInsets.only(top: 10),
      child: Column(
        children: <Widget>[
          _myListTile('领取优惠券'),
          _myListTile('已领取优惠券'),
          _myListTile('地址管理'),
          _myListTile('客服电话'),
          _myListTile('关于我们'),
        ],
      ),
    );
  }
}
